package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Course;
import com.shixulin.mapper.CourseMapper;
import com.shixulin.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Course)表服务实现类
 *
 * @author makejava
 * @since 2023-11-12 20:04:44
 */
@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private
    CourseMapper courseMapper;
    @Override
    public ResponseResult insertCourseInfo(Course course) {
        try {
            courseMapper.insert(course);
            return new ResponseResult(300, "文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500, "文件上传失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseResult getCourseInfo(int id) {

        try{
            QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_id",id);
            List<Course>courseInfos = courseMapper.selectList(queryWrapper);
            if (courseInfos.size() > 0) {
                return new ResponseResult(300,"查询成功",courseInfos);
            }else {
                return new ResponseResult(301,"没有记录");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(405,"查询报错");
        }

    }

    @Override
    public ResponseResult changeCourseInfo( Course course) {
        try {
            int flag = courseMapper.updateById(course);
            if(flag>0){
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(501,"修改失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(401,"错误");
        }


    }

    @Override
    public ResponseResult deleteCourseInfo(int id) {
        try{
            int flag = courseMapper.deleteById(id);
            if(flag>0){
                return new ResponseResult(300,"删除成功");
            }else {
                return new ResponseResult(501,"删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(401,"错误");
        }
    }

    @Override
    public String getalyFilename(String id) {
        Course course = courseMapper.selectById(id);
        if(course!=null){
            return course.getAlyfilename();
        }else {
            return null;
        }
    }
}
